package spring.security.core.util;

import java.io.UnsupportedEncodingException;

public class CharUtils {
	public static char toChar(byte b) {
		return (char) (b & 255);
	}

	public static final byte[] toSimpleByteArray(char[] carr) {
		byte[] barr = new byte[carr.length];

		for (int i = 0; i < carr.length; ++i) {
			barr[i] = (byte) carr[i];
		}

		return barr;
	}

	public static final byte[] toSimpleByteArray(CharSequence charSequence) {
		byte[] barr = new byte[charSequence.length()];

		for (int i = 0; i < barr.length; ++i) {
			barr[i] = (byte) charSequence.charAt(i);
		}

		return barr;
	}

	public static final char[] toSimpleCharArray(byte[] barr) {
		char[] carr = new char[barr.length];

		for (int i = 0; i < barr.length; ++i) {
			carr[i] = (char) (barr[i] & 255);
		}

		return carr;
	}

	public static final int toAscii(char c) {
		return c <= 255 ? c : 63;
	}

	public static final byte[] toAsciiByteArray(char[] carr) {
		byte[] barr = new byte[carr.length];

		for (int i = 0; i < carr.length; ++i) {
			barr[i] = (byte) (carr[i] <= 255 ? carr[i] : 63);
		}

		return barr;
	}

	public static final byte[] toAsciiByteArray(CharSequence charSequence) {
		byte[] barr = new byte[charSequence.length()];

		for (int i = 0; i < barr.length; ++i) {
			char c = charSequence.charAt(i);
			barr[i] = (byte) (c <= 255 ? c : 63);
		}

		return barr;
	}

	public static final byte[] toRawByteArray(char[] carr) {
		byte[] barr = new byte[carr.length << 1];
		int i = 0;

		for (int bpos = 0; i < carr.length; ++i) {
			char c = carr[i];
			barr[bpos++] = (byte) ((c & '＀') >> 8);
			barr[bpos++] = (byte) (c & 255);
		}

		return barr;
	}

	public static final char[] toRawCharArray(byte[] barr) {
		int carrLen = barr.length >> 1;
		if (carrLen << 1 < barr.length) {
			++carrLen;
		}

		char[] carr = new char[carrLen];
		int i = 0;

		char c;
		for (int j = 0; i < barr.length; carr[j++] = c) {
			c = (char) (barr[i] << 8);
			++i;
			if (i != barr.length) {
				c = (char) (c + (barr[i] & 255));
				++i;
			}
		}

		return carr;
	}

	public static final byte[] toByteArray(char[] carr) throws UnsupportedEncodingException {
		return (new String(carr)).getBytes("UTF-8");
	}

	public static final byte[] toByteArray(char[] carr, String charset) throws UnsupportedEncodingException {
		return (new String(carr)).getBytes(charset);
	}

	public static final char[] toCharArray(byte[] barr) throws UnsupportedEncodingException {
		return (new String(barr, "UTF-8")).toCharArray();
	}

	public static final char[] toCharArray(byte[] barr, String charset) throws UnsupportedEncodingException {
		return (new String(barr, charset)).toCharArray();
	}

	public static final boolean equalsOne(char c, char[] match) {
		char[] arg1 = match;
		int arg2 = match.length;

		for (int arg3 = 0; arg3 < arg2; ++arg3) {
			char aMatch = arg1[arg3];
			if (c == aMatch) {
				return true;
			}
		}

		return false;
	}

	public static final int findFirstEqual(char[] source, int index, char[] match) {
		for (int i = index; i < source.length; ++i) {
			if (equalsOne(source[i], match)) {
				return i;
			}
		}

		return -1;
	}

	public static final int findFirstEqual(char[] source, int index, char match) {
		for (int i = index; i < source.length; ++i) {
			if (source[i] == match) {
				return i;
			}
		}

		return -1;
	}

	public static final int findFirstDiff(char[] source, int index, char[] match) {
		for (int i = index; i < source.length; ++i) {
			if (!equalsOne(source[i], match)) {
				return i;
			}
		}

		return -1;
	}

	public static final int findFirstDiff(char[] source, int index, char match) {
		for (int i = index; i < source.length; ++i) {
			if (source[i] != match) {
				return i;
			}
		}

		return -1;
	}

	public static final boolean isWhitespace(char c) {
		return c <= 32;
	}

	public static final boolean isLowercaseAlpha(char c) {
		return c >= 97 && c <= 122;
	}

	public static final boolean isUppercaseAlpha(char c) {
		return c >= 65 && c <= 90;
	}

	public static final boolean isAlphaOrDigit(char c) {
		return isDigit(c) || isAlpha(c);
	}

	public static final boolean isWordChar(char c) {
		return isDigit(c) || isAlpha(c) || c == 95;
	}

	public static final boolean isPropertyNameChar(char c) {
		return isDigit(c) || isAlpha(c) || c == 95 || c == 46 || c == 91 || c == 93;
	}

	public static final boolean isAlpha(char c) {
		return c >= 97 && c <= 122 || c >= 65 && c <= 90;
	}

	public static final boolean isDigit(char c) {
		return c >= 48 && c <= 57;
	}

	public static final boolean isHexDigit(char c) {
		return c >= 48 && c <= 57 || c >= 97 && c <= 102 || c >= 65 && c <= 70;
	}

	public static final boolean isGenericDelimiter(int c) {
		switch (c) {
		case 35:
		case 47:
		case 58:
		case 63:
		case 64:
		case 91:
		case 93:
			return true;
		default:
			return false;
		}
	}

	public static final boolean isSubDelimiter(int c) {
		switch (c) {
		case 33:
		case 36:
		case 38:
		case 39:
		case 40:
		case 41:
		case 42:
		case 43:
		case 44:
		case 59:
		case 61:
			return true;
		case 34:
		case 35:
		case 37:
		case 45:
		case 46:
		case 47:
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 58:
		case 60:
		default:
			return false;
		}
	}

	public static final boolean isReserved(char c) {
		return isGenericDelimiter(c) || isSubDelimiter(c);
	}

	public static final boolean isUnreserved(char c) {
		return isAlpha(c) || isDigit(c) || c == 45 || c == 46 || c == 95 || c == 126;
	}

	public static final boolean isPchar(char c) {
		return isUnreserved(c) || isSubDelimiter(c) || c == 58 || c == 64;
	}

	public static final char toUpperAscii(char c) {
		if (isLowercaseAlpha(c)) {
			c = (char) (c - 32);
		}

		return c;
	}

	public static final char toLowerAscii(char c) {
		if (isUppercaseAlpha(c)) {
			c = (char) (c + 32);
		}

		return c;
	}
}
